import type { AccountInfo, Session, Message, ExportResult, Contact, WeChatInfoList } from '../types/wechat';
import * as App from '../wailsjs/go/main/App';

// Wails v2 提供的全局 window.go
// 调用方式：window.go.main.App.MethodName()

declare global {
  interface Window {
    go: {
      main: {
        App: any;
      }
    }
  }
}

export async function getAllAccounts(): Promise<AccountInfo[]> {
  // 使用 Wails 生成的绑定函数
  const res = await App.GetWeChatAllInfo();
  const obj: WeChatInfoList = JSON.parse(res);
  
  // 获取导出路径状态以确定正确的基路径
  let exportBasePath = './';
  try {
    const exportPathRes = await App.GetExportPathStat();
    const exportPathObj = JSON.parse(exportPathRes);
    if (exportPathObj.path) {
      exportBasePath = exportPathObj.path;
    }
  } catch (error) {
    console.warn('无法获取导出路径，使用默认路径');
  }
  
  // 将 WeChatInfo 转换为 AccountInfo
  return (obj.Info || []).map(info => {
    // 构建导出路径，确保使用正确的路径分隔符
    const userPath = exportBasePath.endsWith('\\') || exportBasePath.endsWith('/') 
      ? `${exportBasePath}User\\${info.AcountName}`
      : `${exportBasePath}\\User\\${info.AcountName}`;
    
    return {
      id: info.AcountName,
      nickname: info.AcountName,
      version: info.Version,
      pid: info.PID,
      wechatVersion: info.Version,
      wechatPath: info.FilePath,
      exportPath: userPath,
      freeSpace: '未知', // 需要其他API获取
      wechatId: info.AcountName,
      decryptResult: info.DBkey ? '解密成功' : '解密失败'
    };
  });
}

export async function exportAccountData(accountId: string, full: boolean = false): Promise<ExportResult> {
  try {
    await App.ExportWeChatAllData(full, accountId);
    return { success: true, message: '导出任务已启动' };
  } catch (error: any) {
    return { success: false, message: error.message || '导出失败' };
  }
}

export async function getSessionList(_accountId: string): Promise<Session[]> {
  const res = await App.GetWechatSessionList(0, 100);
  const obj = JSON.parse(res);
  return obj.SessionList || [];
}

export async function getSessionMessages(sessionId: string): Promise<Message[]> {
  const res = await App.GetWechatMessageListByTime(sessionId, 0, 100, "desc");
  const obj = JSON.parse(res);
  return obj.MessageList || [];
}

export async function getContactList(_accountId: string): Promise<Contact[]> {
  const res = await App.GetWechatContactList(0, 100);
  const obj = JSON.parse(res);
  return obj.ContactList || [];
}

// 新增：获取应用版本
export async function getAppVersion(): Promise<string> {
  return App.GetAppVersion();
}

// 新增：获取是否首次启动
export async function getAppIsFirstStart(): Promise<boolean> {
  return App.GetAppIsFirstStart();
}

// 新增：获取本地账号信息
export async function getWechatLocalAccountInfo(): Promise<string> {
  return App.GetWechatLocalAccountInfo();
}

// 新增：切换账号
export async function wechatSwitchAccount(account: string): Promise<boolean> {
  return App.WechatSwitchAccount(account);
}

// 新增：获取导出路径状态
export async function getExportPathStat(): Promise<string> {
  return App.GetExportPathStat();
}

// 新增：打开文件或文件夹
export async function openFileOrExplorer(path: string, explorer: boolean = true): Promise<string> {
  return App.OpenFileOrExplorer(path, explorer);
}

// 新增：打开目录选择对话框
export async function openDirectoryDialog(): Promise<string> {
  return App.OpenDirectoryDialog();
}

// 新增：打开日志文件夹
export async function openLogFileExplorer(): Promise<void> {
  return App.OepnLogFileExplorer();
}

// 窗口控制功能现在由系统原生标题栏提供
// 如果需要程序化控制窗口，可以保留这些 API
/*
export async function windowMinimize(): Promise<void> {
  return App.WindowMinimize();
}

export async function windowMaximize(): Promise<void> {
  return App.WindowMaximize();
}

export async function windowUnmaximize(): Promise<void> {
  return App.WindowUnmaximize();
}

export async function windowClose(): Promise<void> {
  return App.WindowClose();
}

export async function windowIsMaximized(): Promise<boolean> {
  return App.WindowIsMaximized();
}
*/ 